package com.xbongbong.saas.domain.entity;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.toolbox.util.Arith;
import com.xbongbong.paas.toolbox.wrap.Entity;
import com.xbongbong.pro.enums.TimeTypeEnum;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.NotNull;

/**
 * @description: 业绩目标实体
 * @Author: hongxiao
 * @date: 2019-01-26 10:07
 * @since v1.0
 */
@Getter
@Setter
@ToString
public class PerformanceEntity extends Entity {
    /**
     * 主键
     */
    private Long id;
    /**
     * 公司ID
     */
    private String corpid;
    /**
     * 指标id，即表tb_performance_set内的id
     */
    private Long setId;
    /**
     * 业绩类型：1员工的业绩 2部门的业绩
     */
    private Integer objectiveType;
    /**
     * 员工userId
     */
    private String userId;
    /**
     * 部门id
     */
    private Long departmentId;
    /**
     * 代表目标财年年度，主要是要考虑财年起始月的变更
     */
    private Integer year;
    /**
     * 目标值，格式：{"year":100,"yearHalfUp":50,"yearHalfDown":50,"season":{"s1":-1,"s2":50,"s3":45,"s4":5},"month":{"m1":-1,"m2":-1,"m3":-1,"m4":-1,"m5":10,"m6":40,"m7":-1,"m8":40,"m9":5,"m10":5,"m11":-1,"m12":-1},"week":{"w1":10,"w2":-1,"w3":5}}
     */
    private JSONObject target;
    /**
     * 创建时间
     */
    private Long addTime;
    /**
     * 更新时间
     */
    private Long updateTime;
    /**
     * 删除标记
     */
    private Integer del;
    /**
     * 指标类型：1系统指标,2自定义指标
     */
    @NotNull(message = "指标类型不能为空")
    @Range(min=1,max=2,message = "指标类型传值异常")
    protected Integer statisticsType;


    /**
     * 获取年、半年目标，年目标直接是目标值
     * @param alias “year”，“yearHalfUp”，“yearHalfDown”
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-18 15:22
     * @since v1.0
     * @version v1.0
     */
    public Double getTargetValue4Y(String alias) {
        Double value = -1D;
        if (target != null) {
            if (TimeTypeEnum.YEAR.getAlias().equals(alias) || TimeTypeEnum.YEAR_HALF_UP.getAlias().equals(alias)
                    || TimeTypeEnum.YEAR_HALF_DOWN.getAlias().equals(alias)) {
                value = target.getDouble(alias);
                value = (value == null)? -1D: Arith.div(value, 1, 2);
                return value;
            }
        }
        return value;
    }

    /**
     * 获取目标值内的个别对象：月、季、周
     * 注：不能获取年相关的目标
     * @param alias “month”、“season”、“week”
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-18 15:23
     * @since v1.0
     * @version v1.0
     */
    public JSONObject getPerTargetObject4MSW(String alias) {
        JSONObject perTarget = new JSONObject();
        if (target != null) {
            perTarget = (JSONObject) target.get(alias);
        }
        return perTarget;
    }

    /**
     * 获取月、季度或周的目标值
     * @param perTarget 月、季度或周的目标对象
     * @param key 为：“m1”、“m2”、....“s1”、“s1”、....“w1”、“w2”、...
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-18 15:24
     * @since
     * @version
     */
    public Double getTargetValue4MSW(JSONObject perTarget, String key) {
        Double value = -1D;
        //各月、各季、各周的目标对象
        if (perTarget != null) {
            value = perTarget.getDouble(key);
            value = (value == null)? -1D: Arith.div(value, 1, 2);
        }
        return value;
    }

    /**
     * 获取年、半年、月、季度或周的目标值
     * @param alias “year”、“yearHalfUp”、“yearHalfDown”、“month”、“season”、“week”
     * @param key 为：“m1”、“m2”、....“s1”、“s1”、....“w1”、“w2”、...
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-18 15:26
     * @since v1.0
     * @version v1.0
     */
    public Double getTargetValue4YMSW(String alias, String key) {
        Double value = -1D;
        if (target != null) {
            if (TimeTypeEnum.YEAR.getAlias().equals(alias) || TimeTypeEnum.YEAR_HALF_UP.getAlias().equals(alias)
                    || TimeTypeEnum.YEAR_HALF_DOWN.getAlias().equals(alias)) {
                value = target.getDouble(alias);
                value = (value == null)? -1D: Arith.div(value, 1, 2);
                return value;
            }
            //各月、各季、各周的目标对象
            JSONObject perTarget = (JSONObject) target.get(alias);
            if (perTarget != null) {
                value = perTarget.getDouble(key);
                value = (value == null)? -1D: Arith.div(value, 1, 2);
            }
        }
        return value;
    }
}
